(function ($) {


    function create(e) {
        let options = $.data(e, 'units').options;
        $(e).empty().attr("id", options.id || options.unitType + "_" + options.stepId)
            .window({
                width: 800,
                height: 500,
                title: options.title,
                iconCls: options.iconCls,
                collapsible: false,
                minimizable: false,
                maximizable: false,
                onOpen: function () {
                    let that = this;
                    let width = 680;
                    let labelWidth = 140;
                    let ff = $('<form></form>').appendTo(e).form({});
                    //步骤名称 固定显示
                    $('<div class="step-form-item"><input></div>').appendTo(ff)
                        .children('input')
                        .textbox({
                            width: width,
                            label: '步骤名称',
                            value: options.describe,
                            labelWidth: labelWidth,
                            labelPosition: 'before',
                            labelAlign: 'right',
                            readonly: true
                        });
                    //渲染组件字段
                    $.each(options.columns, function (index, column) {
                        let item = $('<div class="step-form-item"></div>').appendTo(ff);
                        let input = $('<input id="' + column.field + '">').appendTo(item);
                        if(column.multiline){
                            input.textbox({
                                width: width,
                                height:240,
                                label: column.label,
                                labelWidth: labelWidth,
                                labelPosition: 'before',
                                labelAlign: 'right',
                                multiline: column.multiline
                            });
                        }else{
                            input.textbox({
                                width: width,
                                label: column.label,
                                labelWidth: labelWidth,
                                labelPosition: 'before',
                                labelAlign: 'right',
                            });
                        }
                        if (column.hidden) {
                            item.hide()
                        }
                    });
                    //加载数据
                    options.loader();
                },
            });
    }

    $.fn.units = function (method, _param) {
        //方法调用
        if (typeof method == "string") {
            return $.fn.units.methods[method](this, _param);
        }
        //非方法调用, 初始化/重新渲染
        var param = method || {};
        return this.each(function () {
            //jq 单个需要渲染的对象
            var data = $.data(this, "units");//获取对象units缓存
            if (data) {
                //合并options,自定义参数
                $.extend(data.options, param);
            } else {
                //创建options
                let options = $.extend({}, $.fn.units.defaults, $.fn.units.parseOptions(this), param);
                $.extend(options, SETTINGS[options.unitType]);
                $.data(this, "units", {options: options});
            }
            create(this);
        });
    };

    $.fn.units.methods = {
        options: function (jq,p) {
            return $.data(jq[0], "units").options;
        },
        getData: function (jq) {
            return $.data(jq[0], "units").options.data;
        },
        open: function (jq) {
            return jq.window('open')
        }
    };

    $.fn.units.defaults = {
        id: null,
        unitType: null,
        describe: null,
        stepId: null,
        url: '/zjk/convert/stepUnit',
        columns: [],
        data: {},
        title: null,
        iconCls: null,
        loader: function () {
            let options = this;
            $.get($.rootUrl + options.url + "/" + options.unitType, {stepId: options.stepId}, function (res) {
                if (res.success) {
                    let result = res.result;
                    $.extend(options,{data: result});
                    $.each(options.columns, function (i, column) {
                        let field = column.field;
                        if ($('#' + field).length) {
                            $('#' + field).textbox('setValue', result[field]);
                        }
                    });
                } else {
                    $.messager.alert("警告", res.message);
                }
            });
        }
    };



    $.fn.units.parseOptions = function (selector) {
        //标签属性 data-options 解析
        var options = $.extend({}, $.parser.parseOptions(selector, ["id", "unitType", "describe", "stepId"]));
        return options;
    };

    var SETTINGS = {
        READ_JDBC: {
            title: '表输入',
            iconClas: 'icon-read-jdbc',
            columns: [
                {label: 'stepId', field: 'stepId', multiline: false, hidden: true},
                {label: '数据库连接', field: 'ds', multiline: false},
                {label: '', field: 'tableName', multiline: false, hidden: true},
                {label: 'SQL', field: 'executeSql', multiline: true},
            ]
        },
        JSON: {
            title: 'JSON转换',
            iconClas: 'icon-json',
            columns: [
                {label: 'stepId', field: 'stepId', hidden: true},
                {label: '转换类型', field: 'type'},
                {label: 'json条目名称', field: 'rootAlias'},
                {label: '分页数量', field: 'pageSize'},
                {label: '展示空值', field: 'showNullValue'},
                {label: '常量表名', field: 'tableName'},
                {label: '作为流字段输出', field: 'asOutColumn'},
            ]
        },
        XML: {
            title: 'XML转换',
            iconClas: 'icon-xml',
            columns: [
                {label: 'stepId', field: 'stepId', hidden: true},
                {label: '转换类型', field: 'type'},
                {label: '根节点名称', field: 'rowsAlias'},
                {label: '元素节点名称', field: 'rowAlias'},
                {label: '分页数量', field: 'pageSize'},
                {label: '作为流字段输出', field: 'asOutColumn'},
            ]
        },
        SCRIPT_SQL: {
            title: 'SQL脚本',
            iconClas: 'icon-script-sql',
            columns: [
                {label: 'stepId', field: 'stepId', hidden: true},
                {label: '数据库连接', field: 'ds',},
                {label: '', field: 'tableName', hidden: true},
                {label: 'SQL', field: 'sql', multiline: true},
            ]
        },
        WRITE_JDBC: {
            title: '表输出',
            iconClas: 'icon-write-jdbc',
            columns: [
                {label: 'stepId', field: 'stepId', hidden: true},
                {label: '数据库连接', field: 'ds'},
                {label: '目标表', field: 'targetTable'},
                {label: 'SQL', field: 'sql', multiline: true},
            ]
        },
        WRITE_SAP: {
            title: 'SAP接口输出',
            iconClas: 'icon-write-sap',
            columns: [
                {label: 'stepId', field: 'stepId', hidden: true},
                {label: '客户端', field: 'sapClient'},
                {label: '作业名', field: 'jobName'},
                {label: '自动重发次数', field: 'autoRepeatNum'},
                {label: '自动重发间隔(秒)', field: 'autoRepeatTime'},
            ]
        },

    };

})(jQuery);
